package kr.co.insoft.publicmesh.controller.callback;

import kr.co.insoft.core.model.callback.CallBackParamModel;
import kr.co.insoft.core.model.callback.CallBackResultModel;
import kr.co.insoft.core.service.callback.CallBackService;
import kr.co.insoft.framework.exceptions.CallBackServiceException;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "v{callbackVersion}/callback/{callbackSystem:sa|sp}/{callbackType:event|report|cmd}/{callbackInitiator:application|instance}", method = RequestMethod.GET, produces = { "application/json" })
@ResponseStatus(HttpStatus.OK)
public class CallBackController {

	@Autowired
	CallBackService callBackService;

	@RequestMapping(value = "{callbackCommand:stop|status|event|test|install|creating|created|stopping|stopped|regist|deregist|initialize}")
	public CallBackResultModel mainControl(
			@PathVariable("callbackVersion") String callbackVersion,
			@PathVariable("callbackSystem") String callbackSystem,
			@PathVariable("callbackType") String callbackType,
			@PathVariable("callbackInitiator") String callbackInitiator,
			@PathVariable("callbackCommand") String callbackCommand,
			@ModelAttribute("callBackParamModel") CallBackParamModel callBackParamModel)
			throws CallBackServiceException {
		try {
			callBackParamModel.setCallbackVersion(callbackVersion);
			callBackParamModel.setCallbackSystem(callbackSystem);
			callBackParamModel.setCallbackType(callbackType);
			callBackParamModel.setCallbackInitiator(callbackInitiator);
			callBackParamModel.setCallbackCommand(callbackCommand);
			return callBackService.doDistribution(callBackParamModel);
		} catch (ServiceException e) {
			throw new CallBackServiceException(
					ExceptionCode.CallBackServiceException, e);
		}
	}
}
